package com.ruoyi.eqmonitor.dispose.resolve;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ruoyi.eqmonitor.dispose.mapper.MonitorDataMapper;
import com.ruoyi.eqmonitor.dtos.OptDeviceDto;
import com.ruoyi.eqmonitor.utils.Constant;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备参数处理
 *
 * @author dh
 */
@Slf4j
@Component
public class ParameterResolve implements MqttMessageResolve {

    private static final Logger dataLogger = LoggerFactory.getLogger("monitor_param");

    @Resource
    private MonitorDataMapper monitorDataMapper;

    @Override
    public boolean support(int msgType) {
        return Constant.MqttMsgType.PARAMETER.getCode() == msgType;
    }

    @Override
    public boolean resolve(String snCode, OptDeviceDto optDevice, JSONArray messageArray) {
        long start = System.currentTimeMillis();
        String version = UUID.fastUUID().toString();
        log.debug("snCode:{}, deviceCode: {}, 消息流水: {},接收到参数实时数据:{}", snCode, optDevice.getDeviceCode(), version, messageArray);
        dataLogger.info("snCode:{}, deviceCode: {}, 消息流水: {},接收到参数实时数据:{}", snCode, optDevice.getDeviceCode(), version, messageArray);
        List<JSONObject> collect = messageArray.parallelStream()
                // 过滤监测指标
                .filter(obj -> {
                    Constant.MonitorInfo attributeCode = Constant.MonitorInfo.getParamInfoByCode(((JSONObject) obj).getStr("attributeCode"));
                    // 可以找到此监测指标 并且需要存储
                    return attributeCode != null && attributeCode.isSave();
                })
                // 转为存储需要的格式
                .map(obj -> {
                    JSONObject object = (JSONObject) obj;
                    JSONObject jsonObject = new JSONObject();
                    Constant.MonitorInfo attributeCode = Constant.MonitorInfo.getParamInfoByCode(((JSONObject) obj).getStr("attributeCode"));
                    jsonObject.set("monitorCode", attributeCode.getCode());
                    jsonObject.set("monitorData", object.getDouble("attributeData"));
                    jsonObject.set("dataUnit", attributeCode.getUnit());
                    jsonObject.set("monitorName", object.getStr("attributeName"));
                    jsonObject.set("monitorTime", object.getStr("updateTime"));
                    return jsonObject;
                })
                .collect(Collectors.toList());
        try {
            if (CollectionUtil.isNotEmpty(collect)) {
                monitorDataMapper.write(snCode, optDevice, new JSONArray(collect), version);
                log.info("snCode:{}, deviceCode: {}, 消息流水: {}, 全部存储成功, 耗时: {}", snCode, optDevice.getDeviceCode(), version, (System.currentTimeMillis() - start));
            }
        } catch (Exception e) {
            log.error("snCode:{}, deviceCode: {}, 消息流水: {}, 存储失败。 \n {}", snCode, optDevice.getDeviceCode(), version, collect, e);
        }
        return true;
    }

}
